package top.zhangjianyong.tools.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.zhangjianyong.tools.service.api.BillService;
import top.zhangjianyong.tools.user.VerifyUser;
import top.zhangjianyong.tools.utils.ApiResponse;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.YearMonth;

/**
 * @author ： zhangjianyong
 * @date ： 2022/7/17 12:28 PM
 */
@RestController
@RequestMapping("/bill")
public class BillController {

    @Autowired
    private BillService billService;

    /**
     * 上传账单文件
     *
     * @param password
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/upload")
    public ApiResponse upload(@RequestParam("password") String password,
                              @RequestParam("file") MultipartFile file) throws Exception {
        return billService.upload(password, file);
    }

    /**
     * 读取解析账单文件
     *
     * @param billFileId
     * @return
     * @throws IOException
     */
    @PostMapping("/readFile/{billFileId}")
    public ApiResponse readFile(@PathVariable("billFileId") Long billFileId) throws IOException {
        return billService.readFile(billFileId);
    }

    /**
     * 账单文件列表
     *
     * @return
     */
    @PostMapping("/list")
    public ApiResponse list() {
        return billService.list();
    }

    /**
     * 删除账单文件
     *
     * @param billFileId
     * @return
     */
    @PostMapping("/del/{billFileId}")
    public ApiResponse del(@PathVariable("billFileId") Long billFileId) {
        return billService.del(billFileId);
    }


    @VerifyUser(login = false)
    @Deprecated
    @PostMapping("/reload")
    public ApiResponse reload() {
        return billService.reload();
    }

    /**
     * 已导入账期
     *
     * @return
     */
    @PostMapping("/dates")
    public ApiResponse dates() {
        return billService.dates();
    }

    /**
     * 日账单
     *
     * @return
     */
    @PostMapping("/daily")
    public ApiResponse daily() {
        return billService.daily();
    }

    /**
     * 月账单
     *
     * @return
     */
    @PostMapping("/month")
    public ApiResponse month() {
        return billService.month();
    }

    /**
     * 日排行
     *
     * @param page
     * @param size
     * @param date
     * @param platform
     * @param inOrOut
     * @return
     */
    @PostMapping("/rank/daily/{page}/{size}")
    public ApiResponse rank(@PathVariable("page") int page,
                            @PathVariable("size") int size,
                            @RequestParam("date") LocalDate date,
                            @RequestParam(value = "platform", required = false) String platform,
                            @RequestParam(value = "inOrOut", required = false) String inOrOut) {

        return billService.rank(date, platform, inOrOut, page, size);
    }

    /**
     * 月排行
     *
     * @param page
     * @param size
     * @param month
     * @param platform
     * @param inOrOut
     * @return
     */
    @PostMapping("/rank/month/{page}/{size}")
    public ApiResponse rank(@PathVariable("page") int page,
                            @PathVariable("size") int size,
                            @RequestParam("month") YearMonth month,
                            @RequestParam(value = "platform", required = false) String platform,
                            @RequestParam(value = "inOrOut", required = false) String inOrOut) {
        return billService.rank(month, platform, inOrOut, page, size);
    }
}
